package org.sample.algorithm;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Modulo {

    public static final int b = 3;

    public static void main(String[] args) {
        test();
    }

    public static int modulo(int a) {
        int c = new BigDecimal(a).divide(new BigDecimal(b), 0, RoundingMode.FLOOR).intValue();
        return (a - c * b);
    }

    public static void test() {
        int a = -7;
        int b = 4;
        double c = (double) a / (double) b;

        int c_fix = new BigDecimal(c).setScale(0, RoundingMode.DOWN).intValue();
        int c_floor = new BigDecimal(c).setScale(0, RoundingMode.FLOOR).intValue();

        System.out.println("c=" + c);
        System.out.println("fix(c)=" + c_fix + "\nfloor(c)=" + c_floor);

        System.out.println("取余=" + (a - c_fix * b));
        System.out.println("取模=" + (a - c_floor * b));

        System.out.println("a % b=" + (a % b));
        System.out.println("Java中'%'为取余运算");
    }

}